package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.ZnxController;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.Moctc;
import com.example.entity.yifei.Mocte;
import com.example.exception.CustomException;
import com.example.mapper.MoctcMapper;
import com.example.mapper.MocteMapper;
import com.example.service.IActFlowCustomService;
import com.example.service.MoctcService;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MoctctlServiceImpl extends ServiceImpl<MoctcMapper, Moctc> implements  IActFlowCustomService {

    @Resource
    private MoctcMapper moctcMapper;

    @Resource
    private MocteMapper mocteMapper;

    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","材料退料单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Autowired
    private ZnxController znxController;
    @Override
    public void startRunTask(String id) {

    }

    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("材料退料单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Moctc moctc = moctcMapper.selectOne(new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, strings[0]).eq(Moctc::getTc002, strings[1]));
        if(moctc == null) {
            throw new CustomException("500", "材料退料单不存在");
        }
        if(state) {  // 审核通过
            moctc.setTc017(3.0);
            moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, strings[0]).eq(Moctc::getTc002, strings[1]));
            checkcopta(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的材料退料单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            moctc.setTc017(2.0);
            moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, strings[0]).eq(Moctc::getTc002, strings[1]));
        }
    }

    public void checkcopta(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Moctc moctc = moctcMapper.selectOne(new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, TA001).eq(Moctc::getTc002, TA002));
        if(moctc == null) {
            throw new CustomException("500", "单据不存在!");
        }
        moctc.setTc009("Y");
        moctcMapper.update(moctc, new LambdaQueryWrapper<Moctc>().eq(Moctc::getTc001, TA001).eq(Moctc::getTc002, TA002));
        Mocte mocte = new Mocte();
        mocte.setTe019("Y");
        mocteMapper.update(mocte, new LambdaQueryWrapper<Mocte>().eq(Mocte::getTe001, TA001).eq(Mocte::getTe002, TA002));
    }

    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }
}
